/*
 * @Author: 高富强
 * @Email: 875627570@qq.com
 * @Date: 2020-06-09 09:06:42
 * @Last Modified by: 高富强
 * @Last Modified time: 2020-06-09 11:13:14
 * @Description: 班级页面 - 班级弹窗选择页
 */


import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_code_framework/base/base_widget.dart';
import 'package:flutter_code_framework/model_data/class/class.dart';
import 'package:flutter_code_framework/utils/global.dart';
import 'package:flutter_sfsymbols/flutter_sfsymbols.dart';

typedef void GetClassIds(String classIds);
class ClassListPopPage extends BaseWidget {
  GetClassIds callback;
  String reClassIds;
  ClassListPopPage({Key key,this.callback,this.reClassIds}) : super(key: key);

  @override
  BaseWidgetState<BaseWidget> getState() {
    return _ClassListPopPageState();
  }
}

class _ClassListPopPageState extends BaseWidgetState<ClassListPopPage> {
  //行政班
   List<Object> xzClassList = [null];
  //教学班
   List<Object> jxClassList = [null];

   String classIds='';
   int xzCountflag=0;
   int jxCountflag=0;
   bool clickFlag=false;

  Widget buildXZGrid() {
    Color _buttonColor= Theme.of(context).primaryTextTheme.button.color;
    //Color.fromRGBO(255,255,255,1);
    Color _textColor=Theme.of(context).primaryTextTheme.headline6.color;
    //Color.fromRGBO(124, 124, 124, 1);
    Color _buttonActiveColor =    Theme.of(context).primaryColor;
    //Color.fromRGBO(57, 135, 110, 1);
    Color _textActiveColor = Theme.of(context).primaryTextTheme.button.color;
    //Color.fromRGBO(255, 255, 255, 1);
                 List<Widget> tiles = [];//先建一个数组用于存放循环生成的widget
               Widget content; //单独一个widget组件，用于返回需要生成的内容widget

                 if(xzClassList.length>1) {
                   for (var item in xzClassList) {
                     ClassData model = item;

                     if(model!=null&&model.name!=null) {
                       tiles.add(
                           Container(
                             color: Theme.of(context).backgroundColor,
                             height: 32,
                               child: Container(
                                 decoration: new BoxDecoration(
                                  // color: Theme.of(context).primaryTextTheme.headline5.color,
                                   borderRadius: BorderRadius.all(Radius.circular(35)),
                                   border: Border.all(
                                       width: 1,
                                       color: Theme.of(context).primaryTextTheme.subtitle2.color.withOpacity(0.7)),
                                 ),
                                 child:
                                 new FlatButton(
                                   splashColor: Colors.transparent,
                                   highlightColor: Colors.transparent,
                                   color: classIds.contains(model.id)?_buttonActiveColor:_buttonColor,//Colors.green
                                   textColor:classIds.contains(model.id)?_textActiveColor: _textColor,
                                   child: new Text('${model.name.length>18?model.name.substring(0,18)+'...':model.name}',
                                       style: TextStyle(color:classIds.contains(model.id)?_textActiveColor: _textColor,fontSize: 16.0)),
                                   onPressed: () {
                                     setState(() {
                                       if(!classIds.contains(model.id)) {
                                         classIds += model.id+',';
                                       }
                                       else if(classIds.contains(model.id)) {
                                         if(widget.reClassIds!=null) {
                                           if(!widget.reClassIds.contains(model.id)) {
                                             classIds = classIds.replaceAll(
                                                 model.id + ',', '');
                                           }
                                         }
                                       }
                                     });
                                     /*showNormalToast(
                                       '班级id:${model.id}',
                                     );*/
                                   },
                                   shape: RoundedRectangleBorder(
                                       borderRadius: BorderRadius.circular(35) //设置圆角
                                   ),
                                 ),

                               ),
                           ),
                       );
                     }
                   }
                 }
                content = new Padding(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: Wrap(
                    spacing: 14,
                    runSpacing: 12,
                    children: tiles,
                  ),
                );
                 return content;
             }
   Widget buildJXGrid() {
     List<Widget> tiles = [];//先建一个数组用于存放循环生成的widget
     Widget content; //单独一个widget组件，用于返回需要生成的内容widget
     Color _buttonColor=Theme.of(context).primaryTextTheme.button.color;
     //Color.fromRGBO(255,255,255,1);
     Color _textColor=Theme.of(context).primaryTextTheme.headline6.color;
     //Color.fromRGBO(124, 124, 124, 1);
     Color _buttonActiveColor =    Theme.of(context).primaryColor;
     //Color.fromRGBO(57, 135, 110, 1);
     Color _textActiveColor =  Theme.of(context).primaryTextTheme.button.color;
     if(jxClassList.length>1) {
       for (var item in jxClassList) {
         ClassData model = item;

         if(model!=null&&model.name!=null) {
           tiles.add(
             Container(
               color: Theme.of(context).backgroundColor,
               height: 32,
               child: Container(
                 decoration: new BoxDecoration(
                   //color: Theme.of(context).primaryTextTheme.headline5.color,
                   borderRadius: BorderRadius.all(Radius.circular(35)),
                   border: Border.all(
                       width: 1,
                       color: Theme.of(context).primaryTextTheme.subtitle2.color.withOpacity(0.7)),
                 ),
                 child:
                 new FlatButton(
                   color: classIds.contains(model.id)?_buttonActiveColor:_buttonColor,//Colors.green
                   textColor:classIds.contains(model.id)?_textActiveColor: _textColor,
                   child: new Text('${model.name.length>18?model.name.substring(0,18)+'...':model.name}',
                       style: TextStyle(color:classIds.contains(model.id)?_textActiveColor: _textColor,fontSize: 16.0)),
                   onPressed: () {
                     setState(() {
                       if(!classIds.contains(model.id)) {
                         classIds += model.id+',';
                       }
                       else if(classIds.contains(model.id)) {
                         classIds =classIds.replaceAll(model.id+',', '');
                       }
                     });
                     /*showNormalToast(
                       '班级id:${model.id}',
                     );*/
                   },
                   shape: RoundedRectangleBorder(
                       borderRadius: BorderRadius.circular(35) //设置圆角
                   ),
                 ),

               ),
             ),
           );
         }
       }
     }
     content = new Padding(
       padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
       child: Wrap(
         spacing: 14,
         runSpacing: 12,
         children: tiles,
       ),
     );
     return content;
   }

  @override
  Widget buildWidget(BuildContext context) {
    //jxClassList.length==1&&xzClassList.length==1
    if(xzCountflag==1||jxCountflag==1) {
      return Container(
        height:getMainWidgetHeight(),
        margin:EdgeInsets.only(left:24),
        child: ListView(
            children: <Widget>[

              SizedBox(height: 20),
              Row(

                children: <Widget>[
            Container(
              child:
              xzCountflag==1?Text('行政班',style: TextStyle(
                          color:
                          Theme.of(context).primaryTextTheme.headline2.color
                          //Colors.black
                          ,
                          //color: Color.fromRGBO(72,72,72,1),
                          fontSize: 24.0)):Text(''),
                )
              ],
              ),
              SizedBox(height: 14),
              Align(
             alignment: Alignment.centerLeft,
            child: buildXZGrid(),),
              SizedBox(height: 20),
              Row(children: <Widget>[
                  Container(
                  child:
                  jxCountflag==1?Text('教学班',style: TextStyle(
                                  color:
                                  Theme.of(context).primaryTextTheme.headline2.color
                                  //Colors.black
                                  ,
                                  //color: Color.fromRGBO(72,72,72,1),
                                  fontSize: 24.0)):Text(''),
                  )
              ],
              ),
              SizedBox(height: 14),
              Align(
                alignment: Alignment.centerLeft,
                child: buildJXGrid(),),
              SizedBox(height: 20),
            ],)
      );
    }
    else
      {
//        return Center(
//          child: CircularProgressIndicator(),
//        );
        return Center(
          child: Text('暂无加入的班级',style:
          TextStyle(color:  Theme.of(context).primaryTextTheme.caption.color
              //Color.fromRGBO(118,118,118,1)
              ,
              fontSize: 14.0)),
        );
      }
  }
  @override
  void initState() {
    super.initState();
    setState(() {
    if(widget.reClassIds!=null){
      classIds=widget.reClassIds;
    }

    });
    loadMoreData();

  }
//加载列表数据
  loadMoreData() async {
    var rs =await G.req.classData.getPopClassList();
    setState(() {
      if (rs.data["xzClassList"].length > 0) {
        xzClassList.addAll(
            rs.data["xzClassList"].map((e) => ClassData.fromJson(e)).toList());
        xzCountflag=1;
      }
      else
        {
          xzCountflag=0;
        }
      if (rs.data["jxClassList"].length > 0) {
        jxClassList.addAll(
            rs.data["jxClassList"].map((e) => ClassData.fromJson(e)).toList());
        jxCountflag=1;
      }
      else
      {
        jxCountflag=0;
      }
    });
  }
  @override
  void onCreate() {
    // log("onCreate");
    setAppTopNavBarVisible(true);
    setTopNavLeftIcon(SFSymbols.xmark);
  }


  @override
  void onPause() {
    // log('onPause');
  }

  @override
  void onResume() {
    // log("onResume");
  }
  @override
  Widget getTopNavRightWidget(BuildContext _contextBaseFunction) {
    return   GestureDetector(
      child: new Text('确定',style: TextStyle(color:Theme.of(context).primaryColor,fontSize: 14.0)),
      onTap: () {
        setState(() {

        });
        // showNormalToast(
        //   '确定后获得classIds,用逗号分隔：'+classIds,
        // );
        widget.callback(classIds);
        //Navigator.pop(context);
      },
    );
  }
}

